﻿using System;
using System.Collections.Generic;
using System.Web;
using PayRoller.Code;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using System.Configuration;
using System.Web.Profile;
using System.Data;
using HySoft.Cash.BaseClass;
using System.Data.SqlClient;

namespace PayRoller.Manage.RuleSet
{
    public class RuleSet : ParentPage, IRequiresSessionState
    {
        // Fields
        protected Button Button1;
        protected CheckBoxList CheckBoxList1;
        protected HtmlForm form1;
        protected HtmlHead Head1;

        // Methods
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (base.Request["Sid"] != null)
            {
                string str = base.Request["Sid"].ToString();
                string commandText = "delete from tSysRuleModule where RuleSid=" + str;
                int num = SqlHelper.ExecuteNonQuery(ConfigurationManager.AppSettings["SQLConnString"], CommandType.Text, commandText);
                foreach (ListItem item in this.CheckBoxList1.Items)
                {
                    if (item.Selected)
                    {
                        string str3 = item.Value;
                        SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("RuleSid", str), new SqlParameter("ModuleCode", str3) };
                        if (SqlHelper.ExecuteNonQuery(ConfigurationManager.AppSettings["SQLConnString"], CommandType.StoredProcedure, "UptRuleModule", commandParameters) > 0)
                        {
                            base.ClientScript.RegisterStartupScript(base.GetType(), "", "<script>alert('Success!');window.location='RuleList.aspx';</script>");
                        }
                    }
                }
            }
        }

        public DataTable GetSysModules()
        {
            DataTable table = new DataTable();
            string commandText = "select ModuleCode,ModuleName from tSysModules where IsValid=1";
            return SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["SQLConnString"], CommandType.Text, commandText).Tables[0];
        }

        public bool IsExistModule(string RuleSid, string ModuleCode)
        {
            string commandText = "select * from tSysRuleModule where RuleSid='" + RuleSid + "' and ModuleCode='" + ModuleCode + "' ";
            DataTable table = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["SQLConnString"], CommandType.Text, commandText).Tables[0];
            return (table.Rows.Count > 0);
        }

        protected override void OnInit(EventArgs e)
        {
            base.m_ModuleCode = "0011";
            base.CheckLogin(true);
            base.CheckPerm();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                DataTable sysModules = this.GetSysModules();
                for (int i = 0; i < sysModules.Rows.Count; i++)
                {
                    string ruleSid = base.Request["Sid"].ToString();
                    string str2 = sysModules.Rows[i]["ModuleCode"].ToString();
                    ListItem item = new ListItem(sysModules.Rows[i]["ModuleName"].ToString(), str2);
                    if (this.IsExistModule(ruleSid, str2))
                    {
                        item.Selected = true;
                    }
                    this.CheckBoxList1.Items.Add(item);
                }
            }
        }

        // Properties
        protected HttpApplication ApplicationInstance
        {
            get
            {
                return this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }
    }

}
